﻿// -----------------------------------------------------------------------
// <copyright file="WorkflowProfileData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db WorkflowProfile
    /// </summary>
    public class WorkflowProfileData
    {
        /// <summary>
        /// Get WorkflowProfile By Id
        /// </summary>
        /// <param name="workflowProfileId">Param workflowProfileId</param>
        /// <returns>Return WorkflowProfile</returns>
        public WorkflowProfileEN GetWorkflowProfileById(int workflowProfileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkflowProfileEN workflowProfile = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetById, workflowProfileId);
                while (reader.Read())
                {
                    workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return workflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all WorkflowProfile
        /// </summary>
        /// <returns>Return list all WorkflowProfile</returns>
        public IList<WorkflowProfileEN> GetWorkflowProfiles()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<WorkflowProfileEN> lstWorkflowProfile = new List<WorkflowProfileEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetAll);
                while (reader.Read())
                {
                    WorkflowProfileEN workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }

                    lstWorkflowProfile.Add(workflowProfile);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstWorkflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db WorkflowProfile
        /// </summary>
        /// <param name="workflowProfile">Paarm WorkflowProfile</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(WorkflowProfileEN workflowProfile)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.ProfileId, workflowProfile.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.WorkflowId, workflowProfile.WorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.NodeId, workflowProfile.NodeId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.SendUserId, workflowProfile.SendUserId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.MainUserId, workflowProfile.MainUserId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.ProgressWorkId, workflowProfile.ProgressWorkId != 0 ? workflowProfile.ProgressWorkId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.IsCurrent, workflowProfile.IsCurrent),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.StartDate, (object)workflowProfile.StartDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.EndDate, (object)workflowProfile.EndDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.Role, workflowProfile.Role),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.RW, workflowProfile.RW),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.SendDepartmentId, workflowProfile.SendDepartmentId != 0 ? workflowProfile.SendDepartmentId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.MainDepartmentId, workflowProfile.MainDepartmentId != 0 ? workflowProfile.MainDepartmentId : (object)DBNull.Value)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db WorkflowProfile
        /// </summary>
        /// <param name="workflowProfile">Param WorkflowProfile</param>
        public void Update(WorkflowProfileEN workflowProfile)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.WorkflowProfileId, workflowProfile.WorkflowProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.ProfileId, workflowProfile.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.WorkflowId, workflowProfile.WorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.NodeId, workflowProfile.NodeId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.SendUserId, workflowProfile.SendUserId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.MainUserId, workflowProfile.MainUserId),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.ProgressWorkId, workflowProfile.ProgressWorkId != 0 ? workflowProfile.ProgressWorkId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.IsCurrent, workflowProfile.IsCurrent),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.StartDate, (object)workflowProfile.StartDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.EndDate, (object)workflowProfile.EndDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.Role, workflowProfile.Role),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.RW, workflowProfile.RW),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.SendDepartmentId, workflowProfile.SendDepartmentId != 0 ? workflowProfile.SendDepartmentId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.MainDepartmentId, workflowProfile.MainDepartmentId != 0 ? workflowProfile.MainDepartmentId : (object)DBNull.Value)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete WorkflowProfile by workflowProfileId
        /// </summary>
        /// <param name="workflowProfileId">param workflowProfileId</param>
        public void Delete(int workflowProfileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.WorkflowProfile.WorkflowProfileId, workflowProfileId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List WorkflowProfile By ProgressWorkId
        /// </summary>
        /// <param name="progressWorkId">Param progressWorkId</param>
        /// <returns>Return List WorkflowProfile</returns>
        public IList<WorkflowProfileEN> GetWorkflowProfileByProgressWorkId(int progressWorkId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<WorkflowProfileEN> lstWorkflowProfile = new List<WorkflowProfileEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetByProgressWorkId, progressWorkId);
                while (reader.Read())
                {
                    WorkflowProfileEN workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }

                    lstWorkflowProfile.Add(workflowProfile);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstWorkflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the workflow profile by main user_ current_ RW.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="current">The current.</param>
        /// <param name="rw">The rw.</param>
        /// <returns>return WorkflowProfileEN</returns>
        public WorkflowProfileEN GetWorkflowProfileByMainUser_Current_RW(int userId, bool current, bool rw)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkflowProfileEN workflowProfile = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetByMainUserCurrentRW, userId, current, rw);
                while (reader.Read())
                {
                    workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return workflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the workflow profile by main user_ current_ R w_ profile.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="current">if set to <c>true</c> [current].</param>
        /// <param name="rw">if set to <c>true</c> [rw].</param>
        /// <param name="profileId">The profile id.</param>
        /// <returns>return WorkflowProfileEN</returns>
        public WorkflowProfileEN GetWorkflowProfileByMainUser_Current_RW_Profile(int userId, bool current, bool rw, int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkflowProfileEN workflowProfile = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetByMainUserCurrentRWProfile, userId, current, rw, profileId);
                while (reader.Read())
                {
                    workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return workflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the workflow profile by current_ profile.
        /// </summary>
        /// <param name="current">if set to <c>true</c> [current].</param>
        /// <param name="profileId">The profile id.</param>
        /// <returns>return WorkflowProfileEN</returns>
        public WorkflowProfileEN GetWorkflowProfileByCurrent_Profile(bool current, int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkflowProfileEN workflowProfile = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetByCurrentProfile, current, profileId);
                while (reader.Read())
                {
                    workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return workflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the workflow profile by main user_ current_ R w_ profile.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="current">if set to <c>true</c> [current].</param>
        /// <param name="profileId">The profile id.</param>
        /// <returns>return WorkflowProfileEN</returns>
        public WorkflowProfileEN GetWorkflowProfileByMainUser_Current_Profile(int userId, bool current, int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                WorkflowProfileEN workflowProfile = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneWorkflowProfileGetByMainUserCurrentProfile, userId, current, profileId);
                while (reader.Read())
                {
                    workflowProfile = new WorkflowProfileEN();

                    if (!reader.IsDBNull(0))
                    {
                        workflowProfile.WorkflowProfileId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        workflowProfile.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        workflowProfile.WorkflowId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        workflowProfile.NodeId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        workflowProfile.SendUserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        workflowProfile.MainUserId = reader.GetInt32(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        workflowProfile.ProgressWorkId = reader.GetInt32(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        workflowProfile.IsCurrent = reader.GetBoolean(7);
                    }

                    if (!reader.IsDBNull(8))
                    {
                        workflowProfile.StartDate = reader.GetDateTime(8);
                    }

                    if (!reader.IsDBNull(9))
                    {
                        workflowProfile.EndDate = reader.GetDateTime(9);
                    }

                    if (!reader.IsDBNull(10))
                    {
                        workflowProfile.Role = reader.GetString(10);
                    }

                    if (!reader.IsDBNull(11))
                    {
                        workflowProfile.RW = reader.GetBoolean(11);
                    }

                    if (!reader.IsDBNull(12))
                    {
                        workflowProfile.SendDepartmentId = reader.GetInt32(12);
                    }

                    if (!reader.IsDBNull(13))
                    {
                        workflowProfile.MainDepartmentId = reader.GetInt32(13);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return workflowProfile;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}